#pragma once
#include "iTransitionHandler.h"


namespace _FOGGE_
{
	namespace _UTIL_
	{
		namespace _FSM_
		{

			template<class T>
			class aTransitionHandler abstract:
				public iTransitionHandler
			{
			public:
				/*!
					state event handler member function type
				*/
				typedef  HRESULT ((T::*TRANSITION_HANDLER_FUNC)) (::_FOGGE_::GeneralVariableVector& gvb);

				HRESULT OnTransition(Transition* ptran,GeneralVariableVector& gvb)
				{

					TransitionHandlerMap::iterator iter_transition = m_TransitionHandlerMap.find(ptran);

					if(iter_transition != m_TransitionHandlerMap.end())
					{//call handler

						TRANSITION_HANDLER_FUNC handler=iter_transition->second;

						return (boost::polymorphic_downcast<T*>(this)->*handler)(gvb);
					}
					return S_FALSE;
				}


			protected:
				aTransitionHandler(void)
				{
				}
				~aTransitionHandler(void)
				{
				}

				void RegisterTransitionHandler(Transition* ptran,TRANSITION_HANDLER_FUNC punc)
				{

					TransitionHandlerMap::iterator iter_transition = m_TransitionHandlerMap.find(ptran);

					if(iter_transition == m_TransitionHandlerMap.end())
					{//insert new transition
						m_TransitionHandlerMap[ptran] = punc;
					}
					else
					{
						assert(!"TransitionHandler is already setted.check it");
					}
				}

			private:

				typedef std::map<Transition*,TRANSITION_HANDLER_FUNC> TransitionHandlerMap;

				TransitionHandlerMap m_TransitionHandlerMap;

			};
		}
	}
}